
package org.serch.action;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.logging.LogManager;

import org.lobobrowser.html.gui.HtmlPanel;

public class SearchAction {

    public static String sendOutput(String query) {
        URL url;
        String data;
        boolean needProxy = false;
        final Properties logProperties = new Properties();
        logProperties.put("handlers", "java.util.logging.MemoryHandler");
        logProperties.put(".level", "SEVERE");
        logProperties.put("java.util.logging.ConsoleHandler.level", "SEVERE");

        try {
            final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            logProperties.store(bytes, HtmlPanel.class.getName());
            new String(bytes.toByteArray());
            LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(bytes.toByteArray()));

            System.setProperty("java.net.useSystemProxies", "true");

            List l = ProxySelector.getDefault().select(new URI("http://www.google.com/"));

            
            for (Iterator iter = l.iterator(); iter.hasNext();) {
                Proxy proxy = (Proxy) iter.next();
                InetSocketAddress addr = (InetSocketAddress) proxy.address();
                if (addr != null) {
                    needProxy = true;
                    break;
                }
            }
            
            InetSocketAddress address = new InetSocketAddress("localhost", 80);
            address.getAddress();

            System.getProperty("http.auth.ntlm.domain");
//            if(needProxy){
                System.setProperty("proxySet", "true");
                System.setProperty("http.proxyHost", "siwebproxy02.apac.nokia.com");
                System.setProperty("http.proxyPort", "8080");
//            }

            data = URLEncoder.encode("q", "UTF-8") + "=" + URLEncoder.encode(query, "UTF-8");
//            url = new URL("http://google.com/movies?near=Banglore&" + data);
            url = new URL("http://google.com/search?" + data);

            URLConnection conn = url.openConnection();
            conn
                .setRequestProperty("User-Agent",
                    "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            StringBuffer buffer = new StringBuffer();
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
            return buffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
